# -*- shell-script -*-

# 40ibm,vpd - Hardware database routines and variables for
#             systems with device-tree, with ibm,vpd properties.

# This file is part of the Linux lsvpd package.

# (C) Copyright IBM Corp. 2002, 2003, 2004, 2005

# Maintained by Martin Schwenke <martins@au.ibm.com>

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
    
# $Id: 40ibm,vpd,v 1.1 2006/04/11 18:38:29 emunson Exp $

[ "$device_tree_device_type" = "chrp" ] || return 0

######################################################################

get_architecture ()
{
    echo "chrp"
}

should_render ()
{
    local dir="$1"
    local ds="$2"
    local ax="$3"
    local yl="$4"

    # Don't render anything without AX.
    [ -n "$ax" ] || return 1

    # lscfg on pSeries doesn't render stuff in the top node.
    [ "$dir" = "$db_bus_dt_dir" ] && return 1

    # Check location codes for things that don't get displayed in the
    # non-system-specific section on pSeries.
    case "$yl" in
	[!U]*[-/][FLMVY]*)
	    return 1
	    ;;
    esac
    
    should_render_basic "$@"
}

do_pre_items ()
{
    ibm_vpd_maybe_render_header "sys0"       "" "System Object"
    ibm_vpd_maybe_render_header "sysplanar0" "" "System Planar"
}

do_post_items ()
{
    local d="$db_bus_dt_dir"

    ibm_vpd_handle_specials "${d}/cpus" "[Ll]2-cache@*" "L2cache" "L2 Cache" "false"
    ibm_vpd_maybe_render_header "mem0" "" "Memory"
    ibm_vpd_handle_specials "${d}/cpus" "PowerPC,*@*" "proc" "Processor" "true"
}

######################################################################

ibm_vpd_handle_specials ()
{
    dir="$1"
    pat="$2"
    prefix="$3"
    ds="$4"
    print_loc="$5" # true/false

    local n=0
    local node loc_file ax yl
    find "$dir" -type d -name "$pat" |
    sort |
    while read node ; do
	ax="${prefix}${n}"

	loc_file="${node}/ibm,loc-code"
	if $print_loc && [ -f "$loc_file" ] ; then
	    read yl <"${loc_file}"
	else
	    yl=""
	fi

	if should_render "$node" "$ds" "$ax" "$yl" ; then
	    lscfg_render_header "$ax" "$yl" "$ds"
	    if $dovpd && [ -n "$yl" ] ; then
		echo
		lscfg_render_vpd_line "YL" "$yl"
		echo
	    fi
	    n=$(($n + 1))
	fi
    done

}

ibm_vpd_maybe_render_header ()
{
    local ax="$1"
    local yl="$2"
    local ds="$3"

    should_render "" "$ds" "$ax" "$yl" && \
	lscfg_render_header "$ax" "$yl" "$ds"
}

